<template>
    <section class="real-app">

      <div class="tab-container">
        <tabs :value="filter" @change="handleChangeTab">
          <tab :label="tab" :index="tab" v-for="tab in stats" :key="tab">
          </tab>
        </tabs>
      </div>
      <input
          v-model="inputContent"
          type="text"
          class="add-input"
          autofocus="autofocus"
          placeholder="接下去要做什么?"
          @keyup.enter="addTodo"
      >
      <Item
          :todo="todo"
          v-for="todo in filteredTodos"
          :key="todo.id"
          @del="deleteTodo"
      />
      <helper
          @togole="togoleFilter"
          :filter="filter"
          :todos="todos"
          @clearAllCompleted="clearAllCompleted"
      ></helper>
      <router-view/>
    </section>
</template>

<script>
import Item from './item.vue'
import Helper from './helper.vue'
let id = 0

export default {
  beforeRouteEnter (to, from, next) {
    console.log('todo before enter', this)
    next(vm => {
      console.log('after enter vm.id is ', vm.id)
    })
  },
  beforeRouteUpdate (to, from, next) {
    console.log('todo update enter')
    next()
  },
  beforeRouteLeave (to, from, next) {
    console.log('todo leave enter')
    if (global.confirm('are you sure ?')) {
      next()
    }
  },
  props: ['id'],
  mounted () {
    console.log(this.tabValue)
  },
  data () {
    return {
      todos: [],
      filter: 'all',
      stats: ['all', 'active', 'completed'],
      inputContent: ''
    }
  },
  components: {
    Item,
    Helper
  },
  computed: {
    filteredTodos () {
      if (this.filter === 'all') {
        return this.todos
      }
      const completed = this.filter === 'completed'
      return this.todos.filter(todo => completed === todo.completed)
    }
  },
  methods: {
    handleChangeTab (value) {
      this.filter = value
    },
    addTodo (e) {
      this.todos.unshift({
        id: id++,
        content: e.target.value.trim(),
        completed: false
      })
      e.target.value = ''
    },
    deleteTodo (id) {
      this.todos.splice(this.todos.findIndex(todo => todo.id == id), 1)
    },
    clearAllCompleted () {
      this.todos = this.todos.filter(todo => !todo.completed)
    },
    togoleFilter (state) {
      this.filter = state
    }
  }
}
</script>

<style lang="stylus" scoped>
.real-app
    width 600px
    margin 0 auto
    box-shadow 0 0 5px #666

.add-input
    position relative
    margin 0
    width 100%
    font-size 24px
    font-family inherit
    font-weight inherit
    line-height 1.4em
    border none
    outline none
    color inherit
    box-sizing border-box
    font-smoothing antialiased
    padding 16px 16px 16px 36px
    border none
    box-shadow inset 0 -2px 1px rgba(0, 0, 0, 0.03)

.tab-container
 background-color: #fff
 padding 0 15px

</style>
